<template>
  <div class="songs-container">
    <div class="tab-bar">
      <span class="item" :class="{active: tag == 0}" @click="tag = 0">全部</span>
      <span class="item" :class="{active: tag == 7}" @click="tag = 7">华语</span>
      <span class="item" :class="{active: tag == 96}" @click="tag = 96">欧美</span>
      <span class="item" :class="{active: tag == 8}" @click="tag = 8">日本</span>
      <span class="item" :class="{active: tag == 16}" @click="tag = 16">韩国</span>
    </div>
    <!-- 底部的table -->
    <table class="el-table playlit-table">
      <thead>
        <th></th>
        <th></th>
        <th>音乐标题</th>
        <th>歌手</th>
        <th>专辑</th>
        <th>时长</th>
      </thead>
      <tbody>
        <tr class="el-table__row"
        v-for="(item, index) in newSongsList"
        :key="item.id"
        >
          <td>{{index + 1}}</td>
          <td>
            <div class="img-wrap">
              <img :src="item.album.picUrl" alt="" />
              <span class="iconfont icon-play" @click="handlePlay(item.id)"></span>
            </div>
          </td>
          <td>
            <div class="song-wrap">
              <div class="name-wrap">
                <span>{{item.name}}</span>
                <span v-if="item.mvid !== 0" class="iconfont icon-mv" @click="$router.push(`/mv?id=${item.mvid}`)"></span>
              </div>
              <span>{{item.alias[0]}}</span>
            </div>
          </td>
          <td>{{item.album.artists[0].name}}</td>
          <td>{{item.album.name}}</td>
          <td>{{item.duration}}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import { leftSongs, playMusic } from '../network/request'
export default {
  name: 'songs',
  data() {
    return {
      newSongsList: [],
      tag: 0
    };
  },
  watch: {
    tag() {
      this.getList()
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await leftSongs(this.tag);
      this.newSongsList = res.data;
      for(let i = 0;i < this.newSongsList.length;i++) {
        let oldtime = this.newSongsList[i].duration;
        let min = parseInt(oldtime / 1000 / 60)
        min = min < 10 ? '0' + min : min
        let se = parseInt((oldtime / 1000) % 60)
        se = se < 10 ? '0' + se : se
        this.newSongsList[i].duration = `${min}:${se}`
      }
    },
    async handlePlay(id) {
      const res = await playMusic(id)
      let url = res.data[0].url;
      this.$parent.musicUrl = url; 
    }
  },
};
</script>

<style >

</style>
